//
//  changjingWarehouseViewController.m
//  LeaClothing
//
//  Created by LeaClothing on 2019/7/17.
//  Copyright © 2019年 LeaClothing. All rights reserved.
//

#import "changjingWarehouseViewController.h"
#import "jilupiandWarehouseCollectionViewCell.h"
#import "yezinghuiSeriesViewController.h"
#import "luodaiClothingTypeViewController.h"

@interface changjingWarehouseViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *warehouseCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *warehouseCollectionViewFlowLayout;
@property (weak, nonatomic) IBOutlet UINavigationItem *warehouseViewNavigationItem;

@property (nonatomic, strong) NSMutableArray *allClothingArrayDS;

@end

@implementation changjingWarehouseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.warehouseCollectionViewFlowLayout.itemSize =CGSizeMake((iPhoneWidth-60)/2, 200);
    [self loadClothingWarehouseViewDateBase];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadClothingWarehouseViewDateBase) name:ClothingWarehouseViewRefresh object:nil];
}
- (void)loadClothingWarehouseViewDateBase{
    NSArray*arrayDS = [ClothingInfoModel bg_find:clothing_tablename limit:0 orderBy:bg_createTimeKey desc:YES];
    _allClothingArrayDS = [NSMutableArray arrayWithArray:arrayDS];
    [_warehouseCollectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _allClothingArrayDS.count+1;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    jilupiandWarehouseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[jilupiandWarehouseCollectionViewCell jilupiandWarehouseCollectionViewCellID] forIndexPath:indexPath];
    cell.clothingModel = indexPath.item==_allClothingArrayDS.count?nil:[_allClothingArrayDS objectAtIndex:indexPath.item];
    cell.isAddCellView = indexPath.item==_allClothingArrayDS.count;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == _allClothingArrayDS.count) {
        [self performSegueWithIdentifier:@"clothingWarehouseNewlyPushid" sender:nil];
        return;
    }
    ClothingInfoModel * model =  [_allClothingArrayDS objectAtIndex:indexPath.item];
    [self performSegueWithIdentifier:@"clothingSeriesViewPushid" sender:model];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if ([segue.identifier isEqualToString:@"clothingWarehouseNewlyPushid"]) {
        luodaiClothingTypeViewController *addTypeView = segue.destinationViewController;
        addTypeView.type = AddClothingType_type;
    }
    
    if ([segue.identifier isEqualToString:@"clothingSeriesViewPushid"]) {
        yezinghuiSeriesViewController *seriesView = segue.destinationViewController;
        seriesView.clothingSeriesModel = (ClothingInfoModel*)sender;
    }
}

@end
